﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using AC.ExtendedRenderer.Toolkit;
using System.Windows.Forms;
using System.Security;
using System.Drawing;

namespace LED_Editor.Model
{
    public class LedXFileHelper
    {
        protected LedXFileHelper()
        {
        }
        private static LedXFileHelper _instance;
        public static LedXFileHelper Instance
        {
            get {
                if (_instance == null)
                    _instance = new LedXFileHelper(); 
                return _instance; }
        }

        public void SaveFile(IEnumerable<Track> tracks, string fullpath)
        {
            dsFrames dataset = new dsFrames();
            LED_Editor.dsFrames.TracksDataTable tracksTable = dataset.Tracks;
            LED_Editor.dsFrames.FramesDataTable framesTable = dataset.Frames;

            foreach (Track item in tracks)
            {
                LED_Editor.dsFrames.TracksRow tracksRow = tracksTable.NewTracksRow();
                tracksRow.Id = item.Id;
                tracksRow.Name = item.Name;
                tracksTable.AddTracksRow(tracksRow);

                foreach (Frame eachFrame in item.Frames)
                {
                    LED_Editor.dsFrames.FramesRow framesRow = framesTable.NewFramesRow();
                    framesRow.StartColor = eachFrame.StartColor.ToArgb() ;
                    framesRow.EndColor = eachFrame.EndColor.ToArgb();
                    framesRow.FrameDuration = eachFrame.FrameDuration;
                    framesRow.TrackId = item.Id;

                    framesTable.AddFramesRow(framesRow);
                }
            }


            try
            {
                dataset.WriteXml(fullpath, XmlWriteMode.WriteSchema);    
            }
            catch (SecurityException ex )
            {
                throw;
            }
            catch (Exception ex)
            {
                throw;
            }
        }



        public IEnumerable<Track> OpenFile(string fullpath)
        {
            dsFrames dataset = new dsFrames();

            dataset.ReadXml(fullpath, XmlReadMode.Auto);

            List<Track> tracksList = new List<Track>();

            foreach (dsFrames.TracksRow item in dataset.Tracks)
            {
                Track track = Track.CreateInstance();
                track.Id = item.Id;
                track.Name = item.Name;

                IEnumerable<dsFrames.FramesRow> frames = from frame in dataset.Frames
                                            where frame.TrackId == item.Id
                                            select frame;

                foreach (dsFrames.FramesRow frameRow in frames)
                {
                    Frame frame = Frame.CreateInstance();
                    frame.FrameDuration = (int)frameRow.FrameDuration;
                    frame.StartColor = Color.FromArgb(frameRow.StartColor);
                    frame.EndColor = Color.FromArgb(frameRow.EndColor);
                    frame.TrackId = frameRow.TrackId;

                    track.Frames.Add(frame); 
                }

                tracksList.Add(track);
            }

            return tracksList;
        }

    }
}
